package com.ray.controller;

import com.alibaba.fastjson2.JSON;
import com.ray.pojo.School;
import com.ray.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class JSONController
{
    @RequestMapping("/json")
    String getJSON(Model model)
    {


        School school = new School();
        school.setName("石家庄铁道大学");
        school.setAddress("石家庄");

        Student student = new Student();
        student.setName("张三");
        student.setAge(15);
        student.setSchool(school);

        // 如果出现乱码可以加上以下内容
        // <mvc:annotation-driven>
        //     <mvc:message-converters register-defaults="true">
        //         <!-- 配置Fastjson支持 -->
        //         <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        //             <property name="supportedMediaTypes">
        //                 <list>
        //                     <value>text/html;charset=UTF-8</value>
        //                     <value>application/json</value>
        //                 </list>
        //             </property>
        //
        //         </bean>
        //     </mvc:message-converters>
        // </mvc:annotation-driven>

        String s = JSON.toJSONString(student);
        System.out.println(s);
        model.addAttribute("msg", s);
        return "hello";
    }
}
